#!/usr/bin/env perl

BEGIN {
	push @INC,".." if -f "../pv3.pm";
	push @INC,$ENV{CODEMGR_LIBS} if exists($ENV{CODEMGR_LIBS});
}

use File::Basename;
use pv3;
use Getopt::Long;
use strict;
use Config;

$::ARG_pv=undef;

sub usage {
	print "
Usage: pv3_dump [-p DIR]

The pv3_dump utility creates an series of SQL commands allowing
the specified PV3 backend database to be ported or moved to
another environment or database architecture.

The following arguments are supported:

-p  By default the file in question containing the versions is
    thought to exist in the current directory. Use this option
    to reference the version file kept elsewhere.

Written by Simon Edwards, (C) 2006-2010 www.advantsys.co.uk
";
	exit 1;
}

if(!GetOptions(
	"pv=s"		=> \$::ARG_pv,
	)) {
	usage;
}

if(defined($::ARG_pv)) {
	if(! -d dirname($::ARG_pv)) {
		die("Directory to use for pv file does not exist.");
	}
	if(! -f $::ARG_pv) {
		die("Filename specified for PV file does not exist.");
	}
}

my %args=();
$args{PVFILE}=$::ARG_pv;
my $res=pv3::dump_database(%args);
if($res->[0]) {
	print "$_\n" foreach(@{$res->[0]});
	exit(0);
} else {
	print STDERR "ERROR: $res->[1]\n";
	exit(1);
}
